package com.jysz.certificate.persistence.dao.system;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.jysz.certificate.persistence.dao.AbstractBaseDao;
import com.jysz.certificate.persistence.model.system.Shop;

@Repository
public class ShopDao extends AbstractBaseDao<Shop, Long>{
	public boolean deleteShop(Long id){
		try {
			this.destory(id);
			return true;
		} catch (Exception e) {
			System.err.println(e.getMessage());;
			return false;
		}
	}
	public void saveShop(Shop shop){
		this.save(shop);
	}
	
	public boolean shopMarkKey(String shopMark){
		List<Shop> list=this.findBy("shopMark", shopMark);
		if(list!=null&&list.size()>0){
			return false;
		} 
		return true;
	}
	public List<Shop> findAll(){
		List<Shop> list = this.findBy("toDelete",1);
		return list;
	}
	public Shop shopNameKey(String shopMark){
		List<Shop> list=this.findBy("shopMark", shopMark);
		if(list!=null&&list.size()>0){
			return list.get(0);
		} 
		return null;
	}
	public Shop shopName(String name){
		List<Shop> list=this.findBy("shopName", name);
		if(list!=null&&list.size()>0){
			return list.get(0);
		} 
		return null;
	}
	
	/**
	 * 查询有生效协议的店铺
	 */
	public List<Shop> findByEnabledTreaty(){
		String hql="from Shop s where s.id in (select t.shop.id from Treaty t where t.startDate<=:startDate and (t.endDate>=:endDate or t.endDate=null) and t.state=1 and t.financial.billing=1) and s.toDelete=1";
		Query q=getSession().createQuery(hql);
		Date now=new Date();
		q.setDate("startDate", now);
		q.setDate("endDate", now);
//		q.setBoolean("state", true);
		return q.list();
	}
	

}
